package MyGO.controller;
import MyGO.Data.Amount;
import MyGO.Data.GlobalData;
import MyGO.DoctorApplication_main;
import MyGO.Main;
import MyGO.controller.MyController.PushWindow;
import MyGO.entity.Doctor;
import MyGO.service.IAmountService;
import MyGO.service.IDoctorService;
import MyGO.service.impl.AmountServiceImpl;
import MyGO.service.impl.DoctorServiceImpl;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.*;

public class LoginControl {
    @FXML
    private TextField tf_amount;

    @FXML
    private TextField tf_passWord;

    private IAmountService amountService = new AmountServiceImpl();
    private IDoctorService doctorService = new DoctorServiceImpl();
    private List<Amount> amounts;
    private List<Doctor> doctors;
    private HashMap<String, Doctor> IDToDoctor;
    private static int status = 4; //0 挂号 1医生 2检查 3药房 4管理

    @FXML
    public void initialize() {
        amounts = amountService.getAllAmount();
        doctors = doctorService.selevtAllDoctor();
        IDToDoctor = doctorService.IDToDoctor();

    }

    @FXML
    void loginAction(ActionEvent event) throws Exception {
        Doctor doctor;
        Stage stage = GlobalData.stage;
        //得到用户输入
        String amountC = tf_amount.getText();
        String passWord = tf_passWord.getText();
        boolean isValid = false;
        //检查账号密码是否正确
        for (Amount amount : amounts) {
            //多余(可能)的账号验证
            if (Objects.equals(amount.getAmount(), amountC)) {
                //多余(可能)的密码验证
                if (Objects.equals(amount.getPassWord(), passWord)) {
                    if (Objects.equals(amount.getID(), "11111111")) {
                        status = 0;
                    } else if (Objects.equals(amount.getID(), "11111112")) {
                        status = 2;
                    }  else if (Objects.equals(amount.getID(),"11111113")){
                        status = 3;
                    }else if (Objects.equals(amount.getID(),"11111114")){
                        status = 4;
                    } else {
                        status = 1;
                        doctor = IDToDoctor.get(amount.getID());
                        GlobalData.doctor = doctor;
                    }
                    //跳转
                    FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/MyGO/fxml/zong.fxml"));
                    Scene scene = new Scene(fxmlLoader.load());
                    stage.setScene(scene);
                    stage.show();
                }
                else{
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setHeaderText("抱歉阿sir,密码错误");
                    tf_passWord.clear();
                    alert.showAndWait();
                }
                isValid = true;
            }
        }
        if (!isValid) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("嗨嗨嗨,账号错了!!!");
            tf_passWord.clear();
            tf_amount.clear();
            alert.showAndWait();
        }
    }

    @FXML
    void registAction(ActionEvent event) throws IOException {   // 注册的指能是门诊医生
        PushWindow.pushWindow("/MyGO/fxml/regist_doctor_view.fxml", "门诊医生注册", 450.0, 250.0,
                RegistDoctorViewController.class, ((controller, stage) -> {
                    controller.setMyStage(stage);
                    controller.setLoginControl(this);
                }));
    }


    public static int getStatus(){
        return status;
    }



}
